package model;

import Repository.Repository;
import Repository.RepositoryFactory;
import constants.SMSConstants;

public class Merged implements SMSObjectState {

	private final String NAME = "Merged";
	RuleSelector ruleSelector;
	NextStateCalculator stateCalculator;
	CalculateNextStateStrategy strategy;
	Repository repository;
	
	
	@Override
	public void execute(SMSObject smsObj) {
		if(smsObj instanceof Lead){
			//System.out.println("Inside Merged state...");
			ruleSelector =  new PrepareRuleSelector(smsObj);
			stateCalculator =  new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj, ruleSelector);
		//	System.out.println("New state..." + newState.getStateName());
			repository = RepositoryFactory.getRepository(SMSConstants.LEAD);
			if(newState.getStateName().equals(NAME)){
				System.out.println("****Lead could not be prepared as it doesnot pass the rules****");
				smsObj.setCurrentState(newState);
				repository.update(smsObj.getSmsObjId(), smsObj);
				return;
			}
			
			//System.out.println("Prepared Lead object " + smsObj.getSmsObjId());
			//System.out.println("Updating lead state...");
			smsObj.setCurrentState(newState);
			repository.update(smsObj.getSmsObjId(), smsObj);
			System.out.println("Successfully prepared lead");
			
		}
		
	}
	
	public String getStateName(){
		return NAME;
	}
}
